package eyrolles;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

public class LookupAction extends Action {

  protected Double getCotation(String symbole) {

    if ( symbole.equalsIgnoreCase("SUNW") ) {

      return new Double(25.00);
    }
    return null;
  }

  public ActionForward execute(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {

    Double cours = null;

    // Cible par defaut
    String cible = new String("succes");
    String symbole = null;

    if ( form != null ) {

      // Utilisation de LookupForm pour obtenir les parametres de la requete
      LookupForm lookupForm = (LookupForm)form;

      symbole = lookupForm.getSymbole();

      cours = getCotation(symbole);
    }

    // Cible en cas d'�chec
    if ( cours == null ) {

      cible = new String("echec");
      ActionMessages erreurs = new ActionMessages();
      erreurs.add(ActionMessages.GLOBAL_MESSAGE,
        new ActionMessage("erreurs.recherche.symbole.inconnu", symbole));

      // Signalement des erreurs a la page d'origine
      if (!erreurs.isEmpty()) {

        saveErrors(request, erreurs);
      }

    }
    else {

      request.setAttribute("COURS", cours);
    }
    // Transmission a la vue appropriee
    return (mapping.findForward(cible));
  }
}
